﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.OA.Biz
{
    public class EnterpriseBiz
    {
        /// <summary>
        /// 获取所有企业信息
        /// </summary>
        /// <returns></returns>
        public static List<Enterprise> GetEnterpriseList()
        {
            try
            {
                var enterpriseList = new Enterprise().FindAll();
                return enterpriseList;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 分页获取企业信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<Enterprise> GetEnterpriseListByPage(int page, int pageSize, ref int count)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var enterpriseList = (from c in ctx.Enterprise orderby c.ID select c);
                    count = enterpriseList.Count();
                    var data = enterpriseList
                        .Skip((page - 1) * pageSize)
                        .Take(pageSize)
                        .ToList().Select(p => new Enterprise().Set(p)).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 添加企业
        /// </summary>
        /// <returns></returns>
        public static bool EnterpriseAdd(int projectID, string enterpriseName, string enterpriseCode, string enterpriseCredit, string enterpriceInfo, string capitalOperation)
        {
            try
            {
                var Enterprise = new Enterprise()
                {
                    CapitalOperation = capitalOperation,
                    EnterpriceInfo = enterpriceInfo,
                    EnterpriseCode = enterpriseCode,
                    EnterpriseCredit = enterpriseCredit
                };
                return Enterprise.PostAdd() != null;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
        }

        /// <summary>
        /// 根据ID获取企业信息
        /// </summary>
        /// <param name="EnterpriseId"></param>
        /// <returns></returns>
        public static Enterprise GetEnterpriseByID(int enterpriseId)
        {
            try
            {
                return new Enterprise().Find(enterpriseId.ToString());
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 修改企业
        /// </summary>
        /// <param name="Enterprise"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static bool EnterpriseEdit(int enterpriseId, int projectID, string enterpriseName, string enterpriseCode, string enterpriseCredit, string enterpriceInfo, string capitalOperation)
        {
            try
            {
                var Enterprise = new Enterprise().Find(enterpriseId.ToString());
                if (Enterprise != null)
                {
                    Enterprise.CapitalOperation = capitalOperation;
                    Enterprise.EnterpriceInfo = enterpriceInfo; // HttpContext.Current.User.Identity.Name
                    Enterprise.EnterpriseName = enterpriseName;
                    Enterprise.EnterpriseCode = enterpriseCode;
                    Enterprise.EnterpriseCredit = enterpriseCode;
                    return Enterprise.PostModify() != null;
                };
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
            return false;
        }

        /// <summary>
        /// 删除企业
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool EnterpriseDelete(int id = 0)
        {
            try
            {
                var Enterprise = new Enterprise().Find(id.ToString());
                if (Enterprise != null)
                {
                    return Enterprise.PostDelete() != null;
                };
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
            return false;
        }
    }
}
